package com.soft.action.sales;

import com.soft.model.*;
import com.soft.service.AddressService;
import com.soft.service.GarbageOrderService;
import com.soft.service.GarbageTypeService;
import com.soft.service.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * @description: 订单
 **/
@Controller
@RequestMapping("/sales/salesManager")
public class SalerOrderAction {


    @Autowired
    private SalesService salesService;

    @Autowired
    private AddressService addressService;

    @Autowired
    private GarbageTypeService garbageTypeService;

    @Autowired
    private GarbageOrderService garbageOrderService;

    // 跳转到会员管理界面
    @RequestMapping("/toOrderManager")
    public String toUserManager(Map<String, Object> map, HttpSession session) {

        Sales sales = (Sales) session.getAttribute("admin");

        List<GarbageOrder> garbageOrders = garbageOrderService.getAllForSales(sales.getBusinessId());

        List<GarbageType> garbageTypeList = garbageTypeService.getAll();
        List<Address> addresses = addressService.getAll();


        map.put("garbageOrders", garbageOrders);

        map.put("addresses", addresses);
        map.put("garbageTypeList", garbageTypeList);
        return "sales/garbageOrderManager";

    }

    //确定订单
    @RequestMapping(value = "/assigneOrder/{orderId}", method = RequestMethod.GET)
    public void assigneOrder(Map<String, Object> map, HttpSession session, HttpServletResponse response,
                             @PathVariable("orderId") Integer orderId) throws IOException {

        PrintWriter out = response.getWriter();

        Sales sales = (Sales) session.getAttribute("admin");
        GarbageOrder garbageOrder = new GarbageOrder();

        garbageOrder.setId(orderId);
        garbageOrder.setSaleId(sales.getId());
        garbageOrder.setStatus(4);

        int result = garbageOrderService.updateSelective(garbageOrder);

        // 添加垃圾上门回收订单
        if (result == 1) {
            System.out.println("委派收货员成功！");
            out.write("<script>alert('委派收货员成功！');"
                    + "window.location.href='/garbage-collection/sales/salesManager/toOrderManager'</script>");
        } else {
            System.out.println("委派收货员失败！");
            out.write("<script>alert('委派收货员失败！');"
                    + "window.location.href='/garbage-collection/sales/salesManager/toOrderManager'</script>");
        }

    }


}